const ShopCartModel = require('../model/shopCart')

exports.add = async (req, res, next) => {
    try {
        // 相同食物执行编辑操作
        let fids = await ShopCartModel.checkFidExist(req.user_info.id, req.fields.fid)
        console.log(fids);
        if (fids.length === 0) {
            req.fields.uid = req.user_info.id
            let result = await ShopCartModel.add(req.fields)
            res.send({ status: 'success', msg: '购物车添加成功', data: { id: result[0].insertId } })
        } else {
            let where = { fid: req.fields.fid, uid: req.user_info.id }
            count = fids[0].count + req.fields.count
            let data = { count }
            let result = await ShopCartModel.edit(data, where)
            res.send({ status: 'success', msg: '购物车添加成功', data: { id: result[0] } })
        }

        // console.log(result);

    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '购物车添加失败' })
    }
}

exports.edit = async (req, res, next) => {
    try {
        let where = { id: req.params.id, uid: req.user_info.id }
        let result = await ShopCartModel.edit(req.fields, where)
        res.send({ status: 'success', msg: '购物车编辑成功', data: { affectedRows: result[0].affectedRows } })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '购物车编辑失败' })
    }
}

exports.delete = async (req, res, next) => {
    try {
        let result = await ShopCartModel.delete(req.params.id, req.user_info.id)
        res.send({ status: 'success', msg: '购物车删除成功', data: { affectedRows: result[0].affectedRows } })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '购物车删除失败' })
    }
}

exports.findByUid = async (req, res, next) => {
    try {
        let result = await ShopCartModel.findByUid(req.user_info.id, req.params.pageIndex)
        let total = await ShopCartModel.findAllByUid(req.user_info.id)
        let totalPage = Math.ceil(total[0].totalCount / 10)
        let totalPrice = total[0].price
        // console.log(totalPage);
        res.send({ status: 'success', msg: '购物车查找成功', data: { list: result, totalPage, totalPrice } })
        // res.send({ status: 'success', msg: '', data: result })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '购物车查找失败' })
    }
}

exports.findAllByUid = async (req, res, next) => {
    try {
        let result = await ShopCartModel.findAllByUid(req.user_info.id)
        res.send({ status: 'success', msg: '购物车总数获取成功', data: result })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '购物车总数获取失败' })
    }
}

exports.setDefault = async (req, res, next) => {
    try {
        // console.log(req.fields);
        let result = await ShopCartModel.setDefault(req.user_info.id, req.fields)
        res.send({ status: 'success', msg: '购物车default设置成功', data: result })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '购物车default设置失败' })
    }
}